package com.lbz.utils;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * 时间工具
 *
 * @Author 林必昭
 * @Date 2019/6/15 22:23
 */
public class DateTimeUtil {

    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    //                                    传入的对象        格式的类型
    public static Date strToDate(String dateTimeStr,String formatDate){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatDate);
        DateTime dateTime = dateTimeFormatter.parseDateTime(formatDate);
        return dateTime.toDate();
    }

    //当不传格式类型的时候，默认为STANDARD_FORMAT类型
    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

}
